package com.wrupple.muba.base.client.module.services.presentation.impl;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.wrupple.muba.common.client.activity.AbstractDesktopModuleController;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.State.DesktopManager;
import com.wrupple.muba.widget.client.view.Task;

public class MubaDesktopManager implements DesktopManager {
	private HomePlace homePlace;
	private JavaScriptObject userObject;
	@Inject
	public MubaDesktopManager(EventBus bus) {
		super();
		this.homePlace = new HomePlace();
	}

	@Override
	public void goTo(DesktopPlace place) {
		AbstractDesktopModuleController.staticget().getPlaceController().goTo(place);
	}

	@Override
	public void exit() {
		goTo(homePlace);
	}

	@Override
	public HomePlace getDefaultPlace() {
		return homePlace;
	}

	@Override
	public DesktopPlace getWhere() {
		return ((DesktopPlace) AbstractDesktopModuleController.staticget().getPlaceController().getWhere()).cloneItem();
	}
	
	@SuppressWarnings("serial")
	public static class HomePlace extends DesktopPlace{
		public static final String HOME_ACTIVITY = "home";
		public HomePlace() {
			super(HOME_ACTIVITY);
		}
		
		@Override
		public void setActivity(String id) {
		}
	}
	
	
	

	/**
	 * wether succesful or not, this is called after its finished
	 */
	static class AuthFinished implements Command {
		@Override
		public void execute() {
			
		}

	}
	
	  @SuppressWarnings("unchecked")
	  public final <T extends JavaScriptObject> T getUser() {
	    return (T) userObject;
	  }

	/*@Override
	public void onAuthenticationCredentialsEntered(AuthenticationCredentialsEnteredEvent event) {
		SessionPropagator sp = new SessionPropagator(event.credentials);
		sp.setFinalizer(new AuthFinished());
		sp.hacerLogin();
	}*/



	@Override
	public AcceptsOneWidget getOutputFeature() {
		return AbstractDesktopModuleController.staticget().getActivityPresenter().getOutputFeature();
	}

	@Override
	public void restoreTask(Task<?, ?> task) {
		AbstractDesktopModuleController.staticget().getActivityPresenter().restore(task);
	}

	@Override
	public void endUserSession() {
		Window.Location.assign("j_spring_security_logout");
	}

	@Override
	public void setUserObject(JavaScriptObject userData) {
		this.userObject=userData;
	}

}
